/*
 * Copyright 2011, Eric Socolofsky / transmote.com. All rights reserved.

 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * 1.	Redistributions of source code must retain the above copyright notice, this list of
 *		conditions and the following disclaimer.
 *
 * 2.	Redistributions in binary form must reproduce the above copyright notice, this list
 * 		of conditions and the following disclaimer in the documentation and/or other materials
 * 		provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY ERIC SOCOLOFSKY / TRANSMOTE.COM ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ERIC SOCOLOFSKY / TRANSMOTE.COM OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Eric Socolofsky / transmote.com.
 */

package com.transmote.psg.events;

/**
 * <p>
 * Event instances represent generic events dispatched by the Processing Scenegraph framework;
 * Event can be subclassed for custom event development.
 * </p>
 * 
 * @author		Eric Socolofsky
 * @see			EventDispatcher
 */
public class Event {
	/**
	 * Event type dispatched when {@link EventDispatcher#dispose()} is called.
	 */
	public static final int DISPATCHER_DISPOSE = 1;
	
	/**
	 * Event type dispatched when a PSprite is added as a child to another PSprite,
	 * via {@link com.transmote.psg.PSprite#addChild(com.transmote.psg.PSprite)}.
	 */
	public static final int ADDED = 2;
	
	/**
	 * Event type dispatched by a PSprite and each of its descendants
	 * when it is added to the display list and becomes visible on-screen.
	 */
	public static final int ADDED_TO_STAGE = 3;
	
	/**
	 * Event type dispatched when a PSprite is removed from its parent,
	 * via {@link com.transmote.psg.PSprite#removeChild(com.transmote.psg.PSprite)}.
	 */
	public static final int REMOVED = 4;
	
	/**
	 * Event type dispatched when a caught error is handled within
	 * the Processing Scenegraph framework.
	 * NOTE: not currently used.
	 */
	public static final int ERROR = 5;
	
	/**
	 * Event type dispatched when a PSprite is initialized,
	 * NOTE: not currently used.
	 */
	public static final int INIT = 6;
	
	/**
	 * Enumerated display list event phase.
	 * Supported:
	 *	<ul>
	 * 		<li>BUBBLING</li>
	 * 		<li>AT_TARGET</li>
	 * 		<li>CAPTURE</li>
	 * 		<li>NOT_ON_DISPLAY_LIST</li>
	 * </ul>
	 */
	public static enum PHASE {
		BUBBLING,
		AT_TARGET,
		CAPTURE,
		NOT_ON_DISPLAY_LIST;
	}
	
	
	Object target = null;
	EventDispatcher currentTarget = null;
	PHASE phase = PHASE.NOT_ON_DISPLAY_LIST;
	
	protected java.awt.event.InputEvent awtEvent = null;
	private int type = -1;
	private boolean propagationStopped = false;
	
	/**
	 * A generic Processing Scenegraph event instance.
	 * @param	type	The event type.
	 */
	public Event (int type) {
		this.type = type;
	}
	
	/**
	 * The event type.
	 * Handlers should switch on this type
	 * to respond accordingly to the event.
	 */
	public int type () {
		return type;
	}
	
	/**
	 * Enumerated display list event phase.
	 * As an Event instance travels through the Processing Scenegraph display list,
	 * its <tt>phase</tt> changes.  If the Event instance is not on the display list,
	 * <tt>phase</tt> is set to {@link PHASE.NOT_ON_DISPLAY_LIST}.
	 */
	public PHASE phase () {
		return phase;
	}
	
	/**
	 * If dispatched by an {@link EventDispatcher}, the EventDispatcher instance
	 * that dispatched this Event instance.
	 * Otherwise, the Object that dispatched this Event instance.
	 */
	public Object target () {
		return target;
	}
	
	/**
	 * The {@link EventDispatcher} currently processing this Event Instance.
	 */
	public EventDispatcher currentTarget () {
		return currentTarget;
	}
	
	/**
	 * The InputEvent instance generated by Processing/AWT,
	 * if applicable (if this Event instance was generated
	 * in response to a mouse or keyboard action).
	 */
	public java.awt.event.InputEvent awtEvent () {
		return awtEvent;
	}
	
	/**
	 * Stop an Event instance traveling through the display list.
	 * Note: Unlike AS3, Processing Scenegraph does not offer a <tt>stopImmediatePropagation()</tt>
	 * 		 method, as each node in the display list (e.g. each PSprite instance)
	 * 		 can only have one event handler.
	 */
	public void stopPropagation () {
		propagationStopped = true;
	}
	
	/**
	 * Returns <tt>true</tt> if {@link #stopPropagation()} has been called.
	 * else, returns <tt>false</tt>.
	 */
	public boolean isPropagationStopped () {
		return propagationStopped;
	}
	
	@Override
	public String toString () {
		return ("Event::"+ type);
	}
}